package ar.edu.unq.wannaplay.pages;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.spring.injection.annot.SpringBean;

import ar.edu.unq.wannaplay.components.WPLink;
import ar.edu.unq.wannaplay.dtos.ProjectDTO;
import ar.edu.unq.wannaplay.pages.abm.projects.AddProjectPage;
import ar.edu.unq.wannaplay.pages.abm.projects.ModifyProjectPage;
import ar.edu.unq.wannaplay.services.ProjectService;

public class ProjectPage extends WebPage {
    private static final long serialVersionUID = 1L;

    @SpringBean(name = "service.projectService")
    private ProjectService projectService;

    public ProjectPage() {
        this.init();
    }

    public void init() {
        this.add(new PageableListView<ProjectDTO>("tablaProyectos", this.getProjectService().findAllDTOs(), 5) {

            private static final long serialVersionUID = 1L;
            PageParameters parameter;

            @Override
            protected void populateItem(final ListItem<ProjectDTO> item) {
                final ProjectDTO project = item.getModelObject();
                item.add(new Label("name", project.getName()));
                item.add(new Label("client", project.getClient().getName()));
                item.add(new Label("dateFrom", project.getInterval().getStart().toString()));
                item.add(new Label("dateTo", project.getInterval().getEnd().toString()));
//                 item.add(new Label("manHour", project.getManHour()));
                
                parameter = new PageParameters();
                parameter.add("project", String.valueOf(project.getId()));
                this.add(new WPLink<ModifyProjectPage>("edit", ModifyProjectPage.class, parameter));
                this.add(new Link<WebPage>("delete") {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public void onClick() {
                        ProjectPage.this.getProjectService().delete(project);
                        ProjectPage.this.init();
                        // this.setResponsePage(EmpleadosPage.this);
                    }
                });
            }
        });
        this.add(new WPLink<AddProjectPage>("agregarProyecto", AddProjectPage.class));
    }

    // setters&getters
    public ProjectService getProjectService() {
        return projectService;
    }

    public void setProjectService(final ProjectService projectService) {
        this.projectService = projectService;
    }
}
